﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunny.UI;

namespace TestProject
{
    public partial class frmDbFirst : UIPage
    {
        DbContexts db =new DbContexts();

        DataTable dt = new DataTable();

        public frmDbFirst()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmDbFirst_Load(object sender, EventArgs e)
        {
            sctxt.Text = "选择或输入[表名],[保存路径],[命名空间]生成实体类文件";

            //获取所有表名
            string strSql = @"SELECT ID,NAME FROM SYSOBJECTS WHERE TYPE='U' ORDER BY ID";
            dt = db.GetDataTable(strSql);

            //加载绑定下拉框数据
            uiComboDataGridView1.DataGridView.Init();//加载
            uiComboDataGridView1.DataGridView.MultiSelect = true;//设置可多选
            uiComboDataGridView1.DataGridView.AddColumn("数据库表名称", "NAME");//添加绑定列
            uiComboDataGridView1.DataGridView.ReadOnly = true;//只读
            uiComboDataGridView1.SelectIndexChange += uiComboDataGridView1_SelectIndexChange;//选择项变更事件
            uiComboDataGridView1.ShowFilter = true;
            uiComboDataGridView1.DataGridView.DataSource = dt;//绑定数据
            uiComboDataGridView1.FilterColumnName = "NAME"; //设置列筛选 不设置则全部列过滤
        }

        /// <summary>
        /// 选择项变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="index"></param>
        private void uiComboDataGridView1_SelectIndexChange(object sender, int index)
        {
            uiComboDataGridView1.Text = dt.Rows[index]["NAME"].ToString();
        }

        /// <summary>
        /// 值变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void uiComboDataGridView1_ValueChanged(object sender, object value)
        {
            //uiComboDataGridView1.Text = "";
            //if (value != null && value is DataGridViewRow)
            //{
            //    DataGridViewRow row = (DataGridViewRow)value;
            //    uiComboDataGridView1.Text = row.Cells["数据库表名称"].Value.ToString();
            //}

            uiComboDataGridView1.Text = "";
            if (value != null && value is DataGridViewSelectedRowCollection)
            {
                DataGridViewSelectedRowCollection collection = (DataGridViewSelectedRowCollection)value;
                foreach (var item in collection)
                {
                    DataGridViewRow row = (DataGridViewRow)item;
                    uiComboDataGridView1.Text += row.Cells["数据库表名称"].Value.ToString();
                    uiComboDataGridView1.Text += ",";//以逗号分割
                }
            }
        }

        /// <summary>
        /// 文本框点击触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_FilePath_Click(object sender, EventArgs e)
        {
            //判断是否选择文件夹
            if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            //待解压目录
            txt_FilePath.Text = this.folderBrowserDialog1.SelectedPath + Path.DirectorySeparatorChar.ToString();
        }

        /// <summary>
        /// 生成实体类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            
            if (string.IsNullOrEmpty(txt_tablename.Text) && string.IsNullOrEmpty(uiComboDataGridView1.Text))
            {
                sctxt.Text = "请选择或输入表名称";
                return;
            }
            if (string.IsNullOrEmpty(txt_FilePath.Text.Trim()))
            {
                sctxt.Text = "请选择保存路径";
                return;
            }
            if (string.IsNullOrEmpty(txt_NameSpace.Text.Trim()))
            {
                sctxt.Text = "请输入命名空间";
                return;
            }
            else
            {
                //三元运算符 判断取值
                var strArray = uiComboDataGridView1.Text.Trim() != "" ? uiComboDataGridView1.Text.Trim() : txt_tablename.Text.Trim();

                var tbName = strArray.Split(',');
                foreach (var item in tbName)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        //生成到指定目录下
                        db.Db.DbFirst.Where(item).IsCreateAttribute(true).CreateClassFile(txt_FilePath.Text.Trim(), txt_NameSpace.Text.Trim());

                        //打开资源管理器并选中文件
                        Process.Start("explorer.exe", "/select," + txt_FilePath.Text.Trim() + item + ".cs");
                    }
                }

                sctxt.Text = "生成完毕";
            }
            
        }
    }
}
